﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tracy.DataModel;
using System.Xml;
using System.Collections.ObjectModel;
using SharedLib.Helpers;
using System.Text.RegularExpressions;
using Tracy.DataAccess;

namespace Tracy
{
    public class ResourceFilter
    {
        public static List<Resource> FilterRawResources(XmlDocument rawDoc, ICollection<Entry> entries, FeedSource source)
        {
            List<Resource> rtn = new List<Resource>();
            foreach (Entry entry in entries)
            {
                rtn.AddRange(FilterRawResources(rawDoc, entry, source));
            }
            return rtn;
        }

        public static bool IsTitleMatched(Entry entry, string title)
        {
            Regex regEx = new Regex(entry.RegExpr.ToUpper());
            string titleToFilter = StringHelper.UnifyStringFormat(title);
            return regEx.Match(titleToFilter).Success;
        }

        public static List<Resource> FilterRawResources(XmlDocument rawDoc, Entry entry, FeedSource source)
        {
            List<Resource> rtn = new List<Resource>();

            Regex regEx = new Regex(entry.RegExpr.ToUpper());
            foreach (XmlElement resourceNode in rawDoc.SelectNodes("result/resource"))
            {
                string title = XmlHelper.GetNodeValue(resourceNode, "title");
                string titleToFilter = StringHelper.UnifyStringFormat(title);
                if (regEx.Match(titleToFilter).Success)//match entry
                {
                    foreach (FilterSet filterSet in TracyFacade.Instance.FilterManager.FilterProvider.GetFilterSets(entry))
                    {
                        if (filterSet.FeedSource.Name == source.Name)
                        {
                            if (IsMatch(titleToFilter, filterSet))
                            {
                                Resource res = new Resource(resourceNode);
                                //Shift xml encoding
                                res.Title = res.Title.Replace("&amp;", "&").Replace("\"", "'");
                                res.Format = filterSet.Format;
                                foreach (FanSubGroup fsg in filterSet.FanSubGroups) res.FanSubGroups.Add(fsg);
                                res.Resolution = filterSet.Resolution;
                                res.Entry = entry;
                                res.FeedSource = source;
                                rtn.Add(res);
                                //resourceNode.SetAttribute("match", "true");
                                //resourceNode.AppendChild(entry.ToXmlNode(rawDoc));
                                //resourceNode.AppendChild(filterSet.ToXmlNode(rawDoc));
                                break;//TODO: currently, only match the first one
                            }
                        }
                    }
                }
            }

            return rtn;
        }

        //static string PreProcessTitle(string title)//TODO: move to sharedlib
        //{
        //    return Microsoft.VisualBasic.Strings.StrConv(title.Replace("【", "[").Replace("】", "]").Replace("×", "X").ToUpper(), Microsoft.VisualBasic.VbStrConv.SimplifiedChinese);
        //}

        static bool IsMatch(string title, FilterSet filterSet)
        {
            if (FilterProvider.IsMatch(title, filterSet.Resolution))
            {
                if (FilterProvider.IsMatch(title, filterSet.Format))
                {
                    foreach (FanSubGroup fsg in filterSet.FanSubGroups)
                    {
                        if (!FilterProvider.IsMatch(title, fsg))
                        {
                            return false;
                        }
                    }
                    return true;
                }
            }
            return false;
        }
    }
}
